<template>
	<a-card :bordered="false" class="mt-2">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="所属项目" name="projectCode">
						<ProjectSelect v-model:value="searchFormState.projectCode" />
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="所属月份" name="belongingMonth">
						<a-select
							v-model:value="searchFormState.belongingMonth"
							placeholder="请选择所属月份"
							:options="monthOptions"
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table ref="tableRef" :columns="columns" :data="loadData" bordered :scroll="{ x: 'max-content' }">
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon>
							<plus-outlined />
						</template>
						新增
					</a-button>
					<a-button type="primary" @click="initialize"> 初始化</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="ratioRef.showDrawer(record.id)">佣金比例</a>
						<a @click="formRef.onOpen(record)">编辑</a>
						<a @click="advanceRef.onOpen(record)">垫付</a>
					</a-space>
				</template>
				<template
					v-else-if="
						column.dataIndex === 'personnelCount' ||
						column.dataIndex === 'caseCount' ||
						column.dataIndex === 'totalArrearsAmount' ||
						column.dataIndex === 'monthlyTargetAmount' ||
						column.dataIndex === 'monthlyAchievementAmount' ||
						column.dataIndex === 'commission' ||
						column.dataIndex === 'advancePayment' ||
						column.dataIndex === 'commissionExclAdv' ||
						column.dataIndex === 'commissionTarget'
					"
				>
					{{ formatNumber(record[column.dataIndex]) }}
				</template>
				<template v-else>
					{{ record[column.dataIndex] }}
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" :key="new Date().getMilliseconds()" @successful="tableRef.refresh(true)" />
	<AdvanceDetail ref="advanceRef" @successful="tableRef.refresh(true)" />
	<Ratio ref="ratioRef" @successful="tableRef.refresh(true)" />
</template>
<script setup>
	import monthlyApi from '@/api/bi/goals/monthly/monthlyApi'
	import Form from '@/views/bi/goals/monthly/form.vue'
	import Ratio from '@/views/bi/goals/monthly/ratio.vue'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import tool from '@/utils/tool'
	import AdvanceDetail from '@/views/bi/goals/monthly/advanceDetail.vue'
	import { message } from 'ant-design-vue'
	import repaymentRecordApi from '@/api/bi/repayment/repaymentRecordApi'

	const tableRef = ref()
	const formRef = ref()
	const advanceRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({
		current: 1,
		size: 10,
		projectCode: undefined,
		belongingMonth: undefined
	})
	const ratioRef = ref()
	const isResetProject = ref(false)
	const monthOptions = tool.dictList('month')
	const columns = [
		{
			title: '项目名称',
			dataIndex: 'projectName'
		},
		{
			title: '所属部门',
			dataIndex: 'deptName'
		},
		{
			title: '所属月份',
			dataIndex: 'belongingMonthName'
		},
		{
			title: '批次',
			dataIndex: 'batchNumber'
		},
		{
			title: '人数',
			dataIndex: 'personnelCount'
		},
		{
			title: '案件数量',
			dataIndex: 'caseCount'
		},
		{
			title: '案件总金额',
			dataIndex: 'totalArrearsAmount'
		},
		{
			title: '月目标金额',
			dataIndex: 'monthlyTargetAmount'
		},
		{
			title: '当月甲方还款金额',
			dataIndex: 'monthlyClientAmount'
		},
		{
			title: '当月分公司上报金额',
			dataIndex: 'monthlySubsidiaryAmount'
		},
		{
			title: '当月还款差额',
			dataIndex: 'monthlyVarianceAmount'
		},
		{
			title: '结佣比例',
			dataIndex: 'commissionRatio'
		},
		{
			title: '佣金',
			dataIndex: 'commission'
		},
		{
			title: '垫付金额',
			dataIndex: 'advancePayment'
		},
		{
			title: '去掉垫付的佣金',
			dataIndex: 'commissionExclAdv'
		},
		{
			title: '佣金目标',
			dataIndex: 'commissionTarget'
		},
		{
			title: '佣金达成率',
			dataIndex: 'commissionAchievementRate'
		},
		{
			title: '月还款达成率',
			dataIndex: 'monRepayAr'
		},
		{
			title: '编辑',
			dataIndex: 'action',
			fixed: 'right',
			align: 'center'
		}
	]

	const formatNumber = (number) => {
		return new Intl.NumberFormat('zh-CN').format(number)
	}
	const dataParameter = ref({
		id: ''
	})
	const initialize = () => {
		repaymentRecordApi.getFirstInfoOfMonth().then((res) => {
			dataParameter.value.id = res
		})
		monthlyApi.initializeApi(dataParameter.value).then()(() => {
			message.success('初始化成功')
		})
	}

	// 分页条件
	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		return monthlyApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}


	const reset = () => {
		searchFormRef.value.resetFields()
		isResetProject.value = true
		setTimeout(() => {
			isResetProject.value = false
		}, 200)
		tableRef.value.refresh(true)
	}
</script>
